
import { isHotkey } from "is-hotkey";
export class KeyboardEvent {
    constructor (editor, container, view) {
        this.editor = editor;
        this.container = container;
        this.view = view
    }
    beforeInputHandler_ (e) {
        const inputType = e.inputType;
        // 阻止默认的输入
        e.preventDefault();
        switch (inputType) {
            case 'insertText': 
                const text = e.data;
                if (text) {
                    this.editor.insertTextAtCursor(text);
                }
                break;
            case 'insertParagraph':
                this.editor.insertTextAtCursor('\n');
                break;

            case 'deleteContentBackward':
                this.editor.deleteTextAtCursor()
                break
        }
        
    }
    selectionchangeHandler_ (selection) {
        if (selection) {
            this.editor.setSelection(selection.anchor, selection.focus)
        }
    }
    addListeners() {
        this.container.addEventListener('beforeinput', this.beforeInputHandler_.bind(this))
        this.view.on('selection-change', this.selectionchangeHandler_.bind(this))
    }
}
export default KeyboardEvent;